package com.mate.cloud;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.List;


/**
 * @description:    RocketMQ 顺序消息
 * @author:         MI
 * @email:          448341911@qq.com
 * @createTime:     2025/7/2 18:53
 * @updateUser:     MI
 * @updateTime:     2025/7/2 18:53
 * @updateRemark:   修改内容
 * @version:        1.0
 */
public class RocketMqProducer {


    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        //事务消息的时候会用到
        DefaultMQProducer producer=new DefaultMQProducer("mate_producer_group");
        producer.setNamesrvAddr("192.168.110.17:9876"); //它会从命名服务器上拿到broker的地址
        producer.start();

        int num=0;
        while(num<20){
            num++;
            //Topic
            //tags -> 标签 （分类） -> (筛选)
            Message message=new Message("mate_test_topic","TagA",("Hello , RocketMQ:"+num).getBytes());

            //消息路由策略
            producer.send(message, new MessageQueueSelector() {
                @Override
                public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
                    // 消息发送会分发给4个队列
                    System.out.println(list.size());
                    // 实现顺序发送
                    return list.get(0);
                }
            },"key-"+num);
        }
    }
}
